import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import { Toast, Dialog } from 'vant'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: () => import('@/views/layout/'),
    children: [
      {
        path: '', // 默认子路由
        name: 'index',
        meta: {
          keepAlive: true
        },
        component: () => import('@/views/index/index')
      },{
        path: '/category',
        name: 'category',
        meta: {
          keepAlive: true
        },
        component: () => import('@/views/index/category')
      },{
        path: '/cart',
        name: 'cart',
        component: () => import('@/views/index/cart')
      },{
        path: '/user',
        name: 'user',
        component: () => import('@/views/index/user')
      }
    ]
  },{
    path: '/chat',
    name: 'chat',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/chat/index')
  },{
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login')
  },{
    path: '/goodsDetail',
    name: 'goodsDetail',
    component: () => import('@/views/goods/detail')
  },{
    path: '/orderList',
    name: 'orderList',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/order/index')
  },{
    path: '/orderDetail',
    name: 'orderDetail',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/order/detail')
  },{
    path: '/applyList',
    name: 'applyList',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/apply/index')
  },{
    path: '/applyDetail',
    name: 'applyDetail',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/apply/detail')
  },{
    path: '/applyEdit',
    name: 'applyEdit',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/apply/apply')
  },{
    path: '/applyTimeLine',
    name: 'applyTimeLine',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/apply/timeline')
  },{
    path: '/orderPreview',
    name: 'orderPreview',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/order/preview')
  },{
    path: '/orderPay',
    name: 'orderPay',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/order/pay')
  },{
    path: '/orderPayResult',
    name: 'orderPayResult',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/order/order/payresult')
  },{
    path: '/userSign',
    name: 'userSign',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userSign')
  },{
    path: '/userScore',
    name: 'userScore',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userScore')
  },{
    path: '/userinfo',
    name: 'userinfo',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userinfo')
  },{
    path: '/userAccount',
    name: 'userAccount',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userAccount')
  },{
    path: '/userAccountDetail',
    name: 'userAccountDetail',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userAccountDetail')
  },{
    path: '/userCollect',
    name: 'userCollect',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userCollect')
  },{
    path: '/userBrowseHistory',
    name: 'userBrowseHistory',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userBrowseHistory')
  },{
    path: '/userCode',
    name: 'userCode',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/user/userCode')
  },{
    path: '/score',
    name: 'score',
    meta: {
      keepAlive: true
    },
    component: () => import('@/views/score')
  },{
    path: '/faq',
    name: 'faq',
    component: () => import('@/views/user/faq')
  },{
    path: '/faq',
    name: 'faq',
    component: () => import('@/views/user/faq')
  },{
    path: '/setting',
    name: 'setting',
    component: () => import('@/views/setting')
  },{
    path: '/password',
    name: 'password',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/setting/password')
  },{
    path: '/aboutus',
    name: 'aboutus',
    component: () => import('@/views/setting/aboutus')
  },{
    path: '/problem',
    name: 'problem',
    component: () => import('@/views/setting/problem')
  },{
    path: '/seckill',
    name: 'seckill',
    component: () => import('@/views/seckill')
  },{
    path: '/coupon',
    name: 'coupon',
    meta: {
      keepAlive: true
    },
    component: () => import('@/views/coupon')
  },{
    path: '/couponDetail',
    name: 'couponDetail',
    component: () => import('@/views/coupon/detail')
  },{
    path: '/search',
    name: 'search',
    meta: {
      useCache: false,
      keepAlive: true
    },
    component: () => import('@/views/search')
  },{
    path: '/searchChoose',
    name: 'searchChoose',
    component: () => import('@/views/search/searchChoose')
  },{
    path: '/address',
    name: 'address',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/address')
  },{
    path: '/addressEdit',
    name: 'addressEdit',
    meta: {
      requireAuth: true
    },
    component: () => import('@/views/address/edit')
  }
]

const router = new VueRouter({
  routes,
  scrollBehavior(to, from,savedPosition) {
    if(savedPosition){
      return savedPosition;
    }else{
      return {x:0,y:0}
    }
  }
})

router.beforeEach((to,from,next)=>{
  document.querySelector('body').setAttribute('style', 'background-color:#f0f0f0')
	if(to.meta && to.meta.requireAuth){
		if(store.state.user){
			next()
		}else{
      //弹框提示
      Dialog.confirm({
        title: '',
        message: "还未登录，前往登录?"
      })
      .then(() => {
        router.push('/login');
      })
		}
	}else{
		next()
	}
})

export default router
